package com.knight.listener;

import com.knight.EventHandlerPool;
import com.knight.handler.EventHandler;
import com.knight.message.EventDate;

import java.util.List;
import java.util.concurrent.ThreadPoolExecutor;

public class AsyncEventListener extends AbstractEventListener{

    /**
     * 专用的处理事件的线程池
     */
    private ThreadPoolExecutor threadPoolExecutor;

    public AsyncEventListener(ThreadPoolExecutor threadPoolExecutor, EventHandlerPool eventHandlerPool) {
        super(eventHandlerPool);
        this.threadPoolExecutor = threadPoolExecutor;
    }
    @Override
    void handleEventDate(List eventHandlerList, EventDate eventDate) {
        for(Object o : eventHandlerList) {
            EventHandler eventHandler = (EventHandler)o;
            threadPoolExecutor.submit(new Runnable() {
                @Override
                public void run() {
                    eventHandler.handler(eventDate);
                }
            });
        }
    }


}
